package maUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

/**
 * @author ytp0235
 * 
 *
 */

public class SortedRecordsF {
	
	private float _nMinValue = 0;
	private int _nMaxLength = 10;
	private ArrayList<RecordUnitF> _aList = new ArrayList<RecordUnitF>();
	private String _sName = null;
	
	
	public float getMinValue()
	{
		return _nMinValue;
	}
	
	public int getSize()
	{
		return _aList.size();
	}
	
	public String getName()
	{
		return _sName;
	}
	
	public void setName(String str)
	{
		_sName = str;
	}
	
	public boolean notFull()
	{
		return _aList.size() < _nMaxLength;
	}
	
	public boolean contains(RecordUnit ru)
	{
		return _aList.contains(ru);
	}
	
	public SortedRecordsF(int length, String name)
	{
		_nMaxLength = length;	
		_sName = name; 
	}
	
	public void sort()
	{
		Collections.sort(_aList, new CompareByValue());
	}
	
	public void add(RecordUnitF ru)
	{
		_aList.add(ru);
		Collections.sort(_aList, new CompareByValue());
		if(_aList.size()>_nMaxLength)
		{
			_aList.remove(_nMaxLength);
		}
		_nMinValue = _aList.get(_aList.size()-1).getValue();
	}
	public void addIfGTZero(RecordUnitF ru)
	{
		if(ru.getValue() <= 0)
			return;
		add(ru);
	}
	public void addWithoutSort(RecordUnitF ru)
	{
		_aList.add(ru);
		if(_aList.size()>_nMaxLength)
		{
			Collections.sort(_aList, new CompareByValue());
			_aList.remove(_nMaxLength);
			_nMinValue = _aList.get(_aList.size()-1).getValue();
		}
	}
	public ArrayList<RecordUnitF> getAll()
	{
//		Collection<RecordUnit> ret = new Collection();// = new Collection();
//		int n = _aList.size();
//		for(int i = 0; i < n ; i++)
//		{
//			ret.add(_aList.get(i));
//		}
		return _aList;
	}
	
	public RecordUnitF getAt(int n)
	{
		return _aList.get(n);
	}
	
	public void printAll()
	{
		RecordUnit  re;
		System.out.println(_sName + "******************************");
		Iterator list = _aList.iterator();
		while(list.hasNext())
		{
			re = (RecordUnit) list.next();
			System.out.println(re.getValue() + "\t:" + re.getStr());
		}
		
//		for(int i = 0; i < _aList.size(); i++)
//		{
//			re = _aList.get(i);
//			System.out.println(re.getValue() + "\t:" + re.getStr());
//		}

	}
	
	public String getAllString()
	{
		RecordUnitF  re;
		String ret = null;
		ret = "****** " + _sName + " ************" + "\r\n";
		for(int i = 0; i < _aList.size(); i++)
		{
			re = _aList.get(i);
			ret += re.getValue() + "\t:" + re.getStr() + "\r\n";
		}
		//ret += "\r\n\r\n";
		return ret;
	}
	
	public String getAllStringCSV()
	{
		RecordUnitF  re;
		String ret = "";
		for(int i = 0; i < _aList.size(); i++)
		{
			re = _aList.get(i);
			ret += re.getValue() + "," + re.getStr() + "\r\n";
		}
		return ret;
	}

}
